<?php

namespace IDock\Insurance\ZSIC\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\ZSIC\ZSICEnums;
use IDock\Insurance\ZSIC\Response\AllZSICResponse;
use IDock\Printer\Premium\PremiumCoveragePrinter;
use IDock\Printer\Premium\PremiumRelationPrinter;
use IDock\Printer\RenewalInquiryPrinter;
use IDock\Printer\VehiclePrinter;

class RenewalInquiryResponse extends AllZSICResponse
{
    public function printerInit(): Response
    {
        $renewalInquiryPrinter = new RenewalInquiryPrinter();

        $data = $this->getBody()['body'] ?? '';
        $baseData = $data['policy']['base'] ?? '';
        $renewalFlag = $baseData['renewalFlag'] ?? '';
        if ($renewalFlag) {
            $renewalInquiryPrinter->setRenewalFlg(true);
        } else {
            $this->printer = $renewalInquiryPrinter;
            return $this;
        }

        //车辆信息
        $vehicle = $data['vehicle'] ?? [];
        $vehiclePrinter = new VehiclePrinter();
        $vehiclePrinter->vehicleName = $vehicle['modelName'] ?? '';
        $vehiclePrinter->vehicleModelCode = $vehicle['modelCode'] ?? '';
//        $vehiclePrinter->power = $vehicle['power'] ?? '';
        $vehiclePrinter->vehiclePrice = $vehicle['purchasePrice'] ?? '';
        $vehiclePrinter->vehicleSeat = $vehicle['seat'] ?? '';
        $vehiclePrinter->vehicleTonnage = floatval($vehicle['tonnage'] ?? 0);
        $vehiclePrinter->vehicleExhaust = $vehicle['capacity'] ?? '';
        $vehiclePrinter->brandName = $vehicle['brand'] ?? '';
        $vehiclePrinter->wholeWeight = $vehicle['weight'] ?? '';
        $vehiclePrinter->vehicleYear = $vehicle['marketDate'] ?? '';
        $vehiclePrinter->energyTypeCode = array_search($vehicle['fuelType'],ZSICEnums::$energyType) ?: 'E01';
        $vehiclePrinter->energyTypeName = IDockEnums::$energyType[$vehiclePrinter->energyTypeCode] ?? '';
        //车架号
        $renewalInquiryPrinter->vin = $vehicle['frameNo'] ?? '';
        //发动机号
        $renewalInquiryPrinter->engineNo = $vehicle['engineNo'] ?? '';
        $renewalInquiryPrinter->licenseNo = $vehicle['licenseNo'] ?? '';
        $renewalInquiryPrinter->firstRegisterDate = $vehicle['firstRegistrationDate'] ? date('Y-m-d',strtotime($vehicle['firstRegistrationDate'])) : '';//初登日期
        $renewalInquiryPrinter->setVehiclePrinter($vehiclePrinter);

        $parties = $data['parties']['party'] ?? [];
        foreach ($parties as $party){
            if($party['insuredFlag'] == 3){
                //车主
                $owner = $party;
                $ownerPrinter = new PremiumRelationPrinter();
                $ownerPrinter = $this->buildUserInfo($ownerPrinter,$owner);
                $renewalInquiryPrinter->setOwnerPrinter($ownerPrinter);
            }elseif ($party['insuredFlag'] == 2){
                //投保人
                $applicantPrinter = new PremiumRelationPrinter();
                $applicantPrinter = $this->buildUserInfo($applicantPrinter,$party);
                $renewalInquiryPrinter->setApplicantPrinter($applicantPrinter);
            }elseif ($party['insuredFlag'] == 1){
                //被保人
                $insuredPrinter = new PremiumRelationPrinter();
                $insuredPrinter = $this->buildUserInfo($insuredPrinter,$party);
                $renewalInquiryPrinter->setInsuredPrinter($insuredPrinter);
            }
        }
        //交强险
        $renewalInquiryPrinter->ciPolicyNo = $baseData['tciPolicyNo'] ?? '';
        $renewalInquiryPrinter->ciStartData = $baseData['tciEffectiveDate'] ?? '';
        $renewalInquiryPrinter->ciEndData = $baseData['tciExpiryDate'] ?? '';

        $renewalInquiryPrinter->biPolicyNo = $baseData['vciPolicyNo'] ?? '';
        $renewalInquiryPrinter->biStartData = $baseData['vciEffectiveDate'] ?? '';
        $renewalInquiryPrinter->biEndData = $baseData['vciExpiryDate'] ?? '';

        $coverageList = $data['policy']['coverages']['coverage'] ?? [];
        $coverageList  = $this->formatArray($coverageList);
        foreach ($coverageList as $policyItem){
            if($policyItem['productCode'] == 'MAUTO'){
                continue;
                //跳驾意险
            }
            //商业险险种
            $coveragePrinter = new PremiumCoveragePrinter();
            $coverageNo = $policyItem['coverageCode'] ?? '';
            $coveragePrinter->coverageCode = array_search($coverageNo, ZSICEnums::$coverageCode);
            if (!$coveragePrinter->coverageCode) {
                continue;
            }
            $coveragePrinter->coverageName = IDockEnums::$coverageCode[$coveragePrinter->coverageCode];
            $coveragePrinter->amount = intval($policyItem['limitAmount'] ?? '');
            $coveragePrinter->unitAmount = intval($policyItem['unitAmount'] ?? '');
            $coveragePrinter->premium = intval($policyItem['premium'] ?? '');
            $renewalInquiryPrinter->setCoveragePrinterList($coveragePrinter);
        }

        $this->printer = $renewalInquiryPrinter;

        return $this;
    }

    public function  buildUserInfo(PremiumRelationPrinter $user,$data)
    {
        $user->name = $data['name'] ?? '';
        $user->mobile = $data['mobile'] ?? '';
        $user->address = $data['address'] ?? '';
        $user->certificateNo = $data['idNumber'] ?? '';
        $user->certificateTypeCode = array_keys(ZSICEnums::$certificateTypeCode, $data['idType'] ?? '')[0] ?? '';
        $user->email = $data['eMail'] ?? '';
        $user->typeCode = $user->certificateTypeCode == '01' ? '01' : '03';
        return $user;
    }
}
